from collections import defaultdict
from sortedcontainers import SortedDict


class StockPrice:

    def __init__(self):
        self.price = SortedDict()  # 价格和出现次数
        self.now = -1
        self.map = dict()  # 时间 价格

    def update(self, timestamp: int, price: int) -> None:
        self.now = max(timestamp, self.now)  # 更新当前时间
        if timestamp in self.map:
            if self.price[self.map[timestamp]] == 1:
                self.price.pop(self.map[timestamp])
            else:
                self.price[self.map[timestamp]] -= 1
        if price in self.price:
            self.price[price] += 1
        else:
            self.price[price] = 1
        self.map[timestamp] = price

    def current(self) -> int:
        return self.map[self.now]

    def maximum(self) -> int:
        return self.price.keys()[-1]

    def minimum(self) -> int:
        return self.price.keys()[0]


# Your StockPrice object will be instantiated and called as such:
# obj = StockPrice()
# obj.update(timestamp,price)
# param_2 = obj.current()
# param_3 = obj.maximum()
# param_4 = obj.minimum()

sd = SortedDict()
sd[3] = 33
sd[1] = 11
sd[2] = 22
for k, v in sd.items():
    print(k, v)

print(sd.keys())
print(sd.keys()[0])
print(sd.keys()[0], sd.keys()[-1])

map = defaultdict(int)
map[10] += 2

print(map[10])